import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/http/api.dart';
import 'package:flutter_app/http/requests.dart';
import 'package:flutter_app/util/toast.dart';
import 'package:flutter_app/widget/buttons.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((_) {
    runApp(new MyApp());
  });
}

class YourApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<YourApp> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text("LinearLayout Example"),
        ),
        body: new Container(
          color: Colors.yellowAccent,
          child: new Column(
            //Column
//            mainAxisSize: MainAxisSize.min,//wrap_content ,不加的话默认为match_parent（MainAxisSize.max）
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            //start==left,center==center,end==right ,
            // spaceEvenly==等比例居中，4个间距一样大（weight=1),spaceAround=等比例居中，6个间距一样大,spaceBetween=中间居中，两边顶边
            children: [
              new Expanded(
                child: new Container(
                  child: new Icon(
                    Icons.access_time,
                    size: 50.0,
                  ),
                  color: Colors.red,
                ),
                flex: 2, //flex == android:layout_weight
              ),
              new Expanded(
                child: new Container(
                  child: new Icon(
                    Icons.pie_chart,
                    size: 100.0,
                  ),
                  color: Colors.blue,
                ),
                flex: 4,
              ),
              new Expanded(
                child: new Container(
                  child: new Icon(
                    Icons.email,
                    size: 50.0,
                  ),
                  color: Colors.green,
                ),
                flex: 6,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
        // This makes the visual density adapt to the platform that you run
        // the app on. For desktop platforms, the controls will be smaller and
        // closer together (more dense) than on mobile platforms.
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: false,
      backgroundColor: Colors.white,
      body: Container(
          child: new Column(
        children: [
          new Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
                new Image.asset('assets/images/login_bg.png', fit: BoxFit.cover,)
            ],
          ),
          new Row(
            children: [
              new Expanded(
                child: new Column(
                  children: [
                    Container(
                      child: Text(
                        "登录",
                        style: TextStyle(
                            fontSize: 24, fontWeight: FontWeight.bold),
                      ),
                      margin: EdgeInsets.fromLTRB(0, 50, 0, 50),
                    ),
                    Container(
                      margin: EdgeInsets.only(bottom: 6, left: 50, right: 50),
                      padding: EdgeInsets.all(10.0),
                      //用户名输入框
                      child: TextField(
                        //控制器
                        //是否自动更正
                        autocorrect: true,
                        //是否自动对焦
                        //              autofocus: true,
                        decoration: InputDecoration(
                          contentPadding: const EdgeInsets.symmetric(
                              vertical: 4.0, horizontal: 15),
                          hintText: '请输入手机号',
                          // contentPadding: EdgeInsets.all(10),
                          border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(10),
                              borderSide: BorderSide.none),
                          filled: true,
                          fillColor: Color(0xfff4f4fb),
                        ),
                        onChanged: (text) {
                          //内容改变的回调
                          print('change $text');
                        },
                        onSubmitted: (text) {
                          //内容提交(按回车)的回调
                          print('submit $text');
                        },
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(bottom: 6, left: 50, right: 50),
                      padding: EdgeInsets.all(10.0),
                      //用户名输入框
                      child: TextField(
                        obscureText: true,
                        //控制器
                        //是否自动更正
                        autocorrect: true,
                        //是否自动对焦
                        //              autofocus: true,
                        decoration: InputDecoration(
                          contentPadding: const EdgeInsets.symmetric(
                              vertical: 4.0, horizontal: 15),
                          hintText: '请输入密码',
                          // contentPadding: EdgeInsets.all(10),
                          border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(10),
                              borderSide: BorderSide.none),
                          filled: true,
                          fillColor: Color(0xfff4f4fb),
                        ),
                        onChanged: (text) {
                          //内容改变的回调
                          print('change $text');
                        },
                        onSubmitted: (text) {
                          //内容提交(按回车)的回调
                          print('submit $text');
                        },
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 50),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Text(
                            "忘记密码？",
                            style: TextStyle(
                                fontSize: 14, color: Color(0xff999999)),
                          ),
                          GradientButton(
                            child: Container(
                              padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
                              child: Row(
                                children: [
                                  new Container(
                                      child: new Text("登录",
                                          style: TextStyle(
                                            fontSize: 18,
                                          )),
                                      margin: const EdgeInsets.only(right: 30)),
                                  new Image.asset(
                                      'assets/images/arrow_right.png')
                                ],
                              ),
                            ),
                            height: 50,
                            width: 142,
                            colors: [Color(0xff0084ff), Color(0xff06a0fa)],
                            onPressed: () {
                              DioUtils.post(
                                HttpConstant.LOGIN,
                                parameters: {},
                                onSuccess: (data) {
                                  print(data);
                                  ToastUtils.show('来了');
                                },
                                onError: (error) {
                                  ToastUtils.show(error);
                                },
                              );
                            },
                            borderRadius: BorderRadius.all(Radius.circular(10)),
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
        ],
      )),
    );
  }
}
